from django.db import models
from django.conf import settings
from django.contrib.auth.models import User


# Create your models here.

class Profile(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name='用户名')
    date_of_birth = models.DateField(blank=True, null=True, verbose_name='出生年月')
    photo = models.ImageField(upload_to='user/%Y/%m/%d/', blank=True, verbose_name='头像')

    def __str__(self):
        return f"Profile for user {self.user.username}"

    class Meta:
        verbose_name_plural = verbose_name = '用户信息'


class Contact(models.Model):
    user_from = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='rel_from_set', on_delete=models.CASCADE,
                                  verbose_name='关注者')
    user_to = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='rel_to_set', on_delete=models.CASCADE,
                                verbose_name='被关注者')
    created = models.DateTimeField(auto_now_add=True, db_index=True, verbose_name='关注时间')

    class Meta:
        ordering = ('-created',)

    def __str__(self):
        return f'{self.user_from} follows {self.user_to}'


User.add_to_class('following',
                  models.ManyToManyField('self', through=Contact, related_name='followers', symmetrical=False))
